<template>
    <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from 'echarts'
require('echarts/theme/macarons')
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '320px'
    },
    Fail: {
      type: Number,
      default: 0
    },
    Success: {
      type: Number,
      default: 0
    },
    Ongoing: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      chart: null,
    }
  },
  watch: {
    Fail: {
      handler () {
        this.chart.dispose()
        this.initPieChart()
      }
    },
    Success: {
      handler () {
        this.chart.dispose()
        this.initPieChart()
      }
    },
    Ongoing: {
      handler () {
        this.chart.dispose()
        this.initPieChart()
      }
    },
  },
  mounted() {
	  this.$nextTick(()=>{
		  this.initPieChart()
	  })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initPieChart() {
      this.chart = echarts.init(this.$el, 'macarons')

      this.chart.setOption({
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b} : {c} ({d}%)'
        },
        legend: {
          orient: 'vertical',
          x: 'left',
          data: ['成功', '进行中', '失败'],
          textStyle: {
            color: ['green', 'orange', 'red']
          }
        },
        title: {
          text: '成功比例图',
          subtext: '调度情况概览',
          left: 'center',
          top: '40%',
          textStyle: {
            fontSize: 26,
            color: '#454c5c',
            align: 'center'
          },
          subtextStyle: {
            fontSize: 13,
            color: '#6c7a89'
          }
        },
        color: ['green', 'orange', 'red'],
        series: [
          {
            name: '调度情况',
            type: 'pie',
            radius: [95, 125],
            center: ['50%', '48%'],
            itemStyle: {
              borderWidth: 5,
              borderColor: '#fff'
            },
            data: [
              { value: this.Success, name: '成功', label: { color: '#15471b' }},
              { value: this.Ongoing, name: '进行中', label: { color: '#b57424' }},
              { value: this.Fail, name: '失败', label: { color: '#5c1313' }}
            ],
            animationEasing: 'cubicInOut',
            animationDuration: 2600
          }
        ]
      })
    }
  }
}
</script>
